package design_pattern.builder;

public class Student2 {
    private final int stuId;//必须
    private final String name;//必须
    private final int age;//可选
    private final int gender;//可选
    private final String address;//可选

    @Override
    public String toString() {
        return "Student2{" +
                "stuId=" + stuId +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender=" + gender +
                ", address='" + address + '\'' +
                '}';
    }

    private Student2(StudentBuilder builder){
        this.stuId = builder.stuId;
        this.name = builder.name;
        this.age = builder.age;
        this.gender = builder.gender;
        this.address = builder.address;
    }


    public int getStuId() {
        return stuId;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public int getGender() {
        return gender;
    }

    public String getAddress() {
        return address;
    }

    public static class StudentBuilder{
        private final int stuId;
        private final String name;
        private int age = 3;
        private int gender = 4;
        private String address = "黄土高坡";

        public StudentBuilder(int stuId,String name){
            this.stuId = stuId;
            this.name = name;
        }
        public StudentBuilder setAge(int age){
            this.age = age;
            return this;
        }
        public StudentBuilder setGender(int gender){
            this.gender = gender;
            return this;
        }
        public StudentBuilder setAddress(String address){
            this.address = address;
            return this;
        }
        public Student2 build(){
            return new Student2(this);
        }
    }

    public static void main(String[] args) {
        Student2 s2 = new StudentBuilder(1, "小明")//必填属性在构造方法中赋值
                .setAge(1)
                .setGender(1)
                .setAddress("123")
                .build();//对象构建完毕的标识，返回Student对象;

        System.out.println(s2);
    }

}